﻿using System.ComponentModel;
using PPCalc.Commands;
using PPCalc.Lib;

namespace PPCalc.ViewModels
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        private PointsCalculator _points;

        public MainPageViewModel()
        {
            Points=new PointsCalculator();
            Points.PropertyChanged += PointsPropertyChanged;
            InitCommand = new DelegateCommand(t =>
                                                  {
                                                      Points.Protein = 0;
                                                      Points.Fat = 0;
                                                      Points.Carbohydrates = 0;
                                                      Points.Fibre = 0;
                                                  } ,
                                              t => Points.Protein != 0 || Points.Fat != 0 || Points.Carbohydrates != 0 ||
                                                   Points.Fibre != 0);
        }

        void PointsPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            InitCommand.RaiseCanExecuteChanged();
        }

        public PointsCalculator Points
        {
            get { return _points; }
            set
            {
                _points = value;
                OnPropertyChanged("Points");
            }
        }

        public DelegateCommand InitCommand { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}